package com.ruoyi.wx.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.TreeEntity;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.wx.entity.PtQuestion;
import com.ruoyi.wx.entity.dto.*;
import com.ruoyi.wx.mapper.PtQuestionMapper;
import com.ruoyi.wx.service.IPtQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 * 实验题目表 服务实现类
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@Service
public class PtQuestionServiceImpl extends ServiceImpl<PtQuestionMapper, PtQuestion> implements IPtQuestionService {

    @Override
    public QueAllDTO getQueAll(Long expId) {
        List<PtQuestion> ptQuestions = baseMapper.selectList(Wrappers.lambdaQuery(PtQuestion.class)
            .eq(PtQuestion::getExpId, expId).eq(PtQuestion::getStatus, "0")
        );

        Map<String, List<PtQuestion>> collect = ptQuestions.stream().collect(Collectors.groupingBy(PtQuestion::getType));
        List<OptQueDTO> singleOptQueDTOList = packageOptQueDTO(collect.get(Constants.QUE_OPT_TYPE));
        List<OptQueDTO> manyOptQueDTOList = packageOptQueDTO(collect.get(Constants.QUE_OPTS_TYPE));
        List<JudgeQueDTO> judgeQueDTOList = packageJudgeQueDTO(collect.get(Constants.QUE_JUDGE_TYPE));
        List<QueQueDTO> queQueDTOList = packageQueQueDTO(collect.get(Constants.QUE_WDA_TYPE));

        QueAllDTO res = new QueAllDTO();
        res.setSingleOptQueDTOList(singleOptQueDTOList);
        res.setManyOptQueDTOList(manyOptQueDTOList);
        res.setJudgeQueDTOList(judgeQueDTOList);
        res.setQueQueDTOList(queQueDTOList);
        return res;
    }

    /**
     * 封装单选和多选的分流操作！
     * @param optQue 流
     * @return 单选或者多选
     */
    public List<OptQueDTO> packageOptQueDTO(List<PtQuestion> optQue) {
        if (optQue == null) {
            return null;
        }

        return optQue.stream().map(e -> {
            OptQueDTO copy = BeanCopyUtils.copy(e, OptQueDTO.class);
            assert copy != null;
            copy.setExplainContent(e.getAnalysis());
            List<OptDTO> optionList = new ArrayList<>();
            optionList.add(new OptDTO("A", e.getOptionA(), e.getType()));
            optionList.add(new OptDTO("B", e.getOptionB(), e.getType()));
            optionList.add(new OptDTO("C", e.getOptionC(), e.getType()));
            optionList.add(new OptDTO("D", e.getOptionD(), e.getType()));
            copy.setOptionList(optionList);
            return copy;
        }).collect(Collectors.toList());
    }

    public List<JudgeQueDTO> packageJudgeQueDTO(List<PtQuestion> optQue) {
        if (optQue == null) {
            return null;
        }
        return optQue.stream().map(e -> {
            JudgeQueDTO copy = BeanCopyUtils.copy(e, JudgeQueDTO.class);
            assert copy != null;
            copy.setExplainContent(e.getAnalysis());
            List<OptDTO> optionList = new ArrayList<>();
            optionList.add(new OptDTO("A", "对", e.getType()));
            optionList.add(new OptDTO("B", "错", e.getType()));
            copy.setOptionList(optionList);
            copy.setAnswer("1".equals(e.getType()) ? "A" : "B");
            return copy;
        }).collect(Collectors.toList());
    }

    public List<QueQueDTO> packageQueQueDTO(List<PtQuestion> optQue) {
        if (optQue == null) {
            return null;
        }
        return optQue.stream().map(e->{
            QueQueDTO copy = BeanCopyUtils.copy(e, QueQueDTO.class);
            assert copy != null;
            copy.setTitle(e.getTitle() + e.getAnswer());
            return copy;
        }).collect(Collectors.toList());
    }
}
